package com.heze.xunhuan.controller;

import com.heze.xunhuan.entity.Report;
import com.heze.xunhuan.service.ReportService;
import com.heze.xunhuan.utils.SecurityContextUtil;
import com.heze.xunhuan.vo.PageVO;
import com.heze.xunhuan.vo.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 举报控制器
 *
 * @author heze-xunhuan
 */
@Tag(name = "举报审核", description = "举报相关接口")
@RestController
@RequestMapping("/api/v1/reports")
@RequiredArgsConstructor
public class ReportController {

    private final ReportService reportService;

    @Operation(summary = "提交举报", description = "提交一条举报")
    @PostMapping
    public Result<Long> submitReport(@RequestBody Map<String, Object> params) {
        Long userId = SecurityContextUtil.getCurrentUserId();
        if (userId == null) {
            return Result.unauthorized("未登录");
        }

        // 参数校验
        if (params.get("targetType") == null) {
            return Result.badRequest("举报对象类型不能为空");
        }
        if (params.get("targetId") == null) {
            return Result.badRequest("举报对象ID不能为空");
        }
        if (params.get("reasonType") == null) {
            return Result.badRequest("举报原因类型不能为空");
        }

        String targetType = params.get("targetType").toString();
        Long targetId;
        try {
            targetId = Long.parseLong(params.get("targetId").toString());
        } catch (NumberFormatException e) {
            return Result.badRequest("举报对象ID格式错误");
        }

        String reasonType = params.get("reasonType").toString();
        String reasonDetail = params.get("reasonDetail") != null ? params.get("reasonDetail").toString() : null;
        Map<String, Object> evidence = params.get("evidence") != null
                ? (Map<String, Object>) params.get("evidence")
                : null;

        Long reportId = reportService.submitReport(userId, targetType, targetId, reasonType, reasonDetail, evidence);
        return Result.success(reportId);
    }
}

